package com.dtzhejiang.social.supervision.risk.engine.core.configuration;

import com.dtzhejiang.social.supervision.risk.engine.common.enums.JobEnums;
import com.dtzhejiang.social.supervision.risk.engine.common.enums.StepEnums;
import com.dtzhejiang.social.supervision.risk.engine.core.mapper.PersonMapper;
import com.dtzhejiang.social.supervision.risk.engine.core.task.tasklet.MyTask;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.SimpleJobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.builder.AbstractTaskletStepBuilder;
import org.springframework.batch.core.step.builder.TaskletStepBuilder;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.interceptor.TransactionAttribute;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 任务，步骤定义配置
 * @Author: Lexus
 * @Date: 2020/12/16 15:44
 */
@Configuration
public class TaskletConfiguration {
    private static int i = 0;
    @Resource
    private PersonMapper personMapper;
    /**
     * 医美定时任务场景总入口
     * @param jobBuilderFactory
     * @return
     */
//
//    @Bean(name="licenseJob")
//    public SimpleJobBuilder licenseJob(JobBuilderFactory jobBuilderFactory, Map<String, TaskletStepBuilder> taskletStepBuilderMap){
//        return jobBuilderFactory.get(JobEnums.LICENSE.getCode()).incrementer(new RunIdIncrementer()).start(taskletStepBuilderMap.get(StepEnums.ONE_STEP.getCode()).build())
//                .next(taskletStepBuilderMap.get(StepEnums.TWO_STEP.getCode()).build());
//    }

    /**
     * 无证无照风险模型任务入口
     *
     * @param jobBuilderFactory
     * @param taskletStepBuilderMap
     * @return
     */
    @Bean(name="licenseJob")
    public SimpleJobBuilder licenseJob(JobBuilderFactory jobBuilderFactory, Map<String, AbstractTaskletStepBuilder<TaskletStepBuilder>> taskletStepBuilderMap) {
        return jobBuilderFactory.get(JobEnums.LICENSE.getCode()).incrementer(new RunIdIncrementer())
                .start(taskletStepBuilderMap.get(StepEnums.ONE_STEP.getCode()).build())
                .next(taskletStepBuilderMap.get(StepEnums.TWO_STEP.getCode()).build());
    }
    /**
     *  一步
     * @param stepBuilderFactory
     * @return
     */
    @Bean(name="oneStep")
    public TaskletStepBuilder oneStep(StepBuilderFactory stepBuilderFactory, MyTask myTask, TransactionAttribute transactionAttribute){
        return stepBuilderFactory.get(StepEnums.SUPERVISE_LA_TA.getCode())
                .tasklet((contribution, chunkContext) -> {
                    System.out.println(111);
                    return RepeatStatus.FINISHED;
                });
    }

    /**
     *  二步
     * @param stepBuilderFactory
     * @return
     */
    @Bean(name="twoStep")
    public AbstractTaskletStepBuilder<TaskletStepBuilder> twoStep(StepBuilderFactory stepBuilderFactory, MyTask myTask, TransactionAttribute transactionAttribute){
        return stepBuilderFactory.get(StepEnums.ONE_STEP.getCode())
                .tasklet((contribution, chunkContext) -> {
                    myTask.myTest();
//                    PersonEntity personEntity = new PersonEntity();
//                    personEntity.setId(202);
//                    personEntity.setName("1233");
//                    personMapper.insert(personEntity);
//                    personEntity.setId(202);
//                    personEntity.setName("1233");
//                    personMapper.insert(personEntity);
                    if (++i < 1) {
                        return RepeatStatus.CONTINUABLE;
                    }else {
                        i=0;
                        return RepeatStatus.FINISHED;
                    }
                })
                .transactionAttribute(transactionAttribute);
    }

//    //只配置builder bean，bean name和枚举类型保持一致
//    @Bean(name="licenseAnalysisStep")
//    public AbstractTaskletStepBuilder<TaskletStepBuilder> licenseAnalysisStep(StepBuilderFactory stepBuilderFactory, LicenseAnalysisTask licenseAnalysisTask, TransactionAttribute transactionAttribute) {
//        return stepBuilderFactory.get(NoLicenseStepEnums.CLEAN_LICENSE_ANALYSIS.getCode())
//                .tasklet((contribution, chunkContext) -> {
//                    licenseAnalysisTask.analysisLicense();
//                    return RepeatStatus.FINISHED;
//                }).transactionAttribute(transactionAttribute);//去除事务
}
